﻿using Core.DataAccess.Model.Project.Information;
using Core.Framework.Model.Common;
using Core.Framework.Util;
using Core.IBusiness.IInformation;
using Core.IBusiness.IInformation.Model;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;

namespace Core.Web.Controllers.Information
{
    /// <summary>
    /// 信息管理
    /// </summary>
    [Route(".v1/[controller]")]
    [ApiController]
    [ApiVerification(IBusiness.IProjectModule.Model.APIServiceEnum.BBS)]
    public class InformationController : BaseApiController
    {
        public InformationController(IInformation iInformation) : 
            base(iInformation: iInformation)
        {
        
        }
        

        #region Serach

        /// <summary>
        /// 查询指定文章信息[包含评论信息]
        /// </summary>
        /// <param name="model">查询实体</param>
        /// <returns></returns>
        [HttpPost(".getitem")]
        [IgnoreAuthorize]
        public IResult PostGetInfo(GetInfoItem model)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iInformation.GetInformationItem(
                        new InformationList
                        {
                            Id = model.infoKey,
                            ProjectToken = iProjectInfo.Token
                        },
                        model.isComment,
                        model.attributes));
                }, model.IsValid(IsValidEnum.select));
        }

        /// <summary>
        /// 搜索文章信息
        /// </summary>
        /// <param name="model">查询实体</param>
        /// <returns></returns>
        [HttpPost(".search")]
        [IgnoreAuthorize]
        public IResult PostGetInfo(SearchInfoRequest model)
        {

            model.LayerTable();

            return this.ExecMethod(
                () => {
                    return this.Result(this.iInformation.GetInformationSearch(
                        model.Type,
                        iProjectInfo.Token,
                        model.pagination,
                        model.search), model.pagination);
                }, model.IsValid(IsValidEnum.select));
        }


        /// <summary>
        /// 搜索文章信息并获取指定属性
        /// </summary>
        /// <param name="model">查询实体</param>
        /// <returns></returns>
        [HttpPost(".searchattrs")]
        [IgnoreAuthorize]
        public IResult PostGetInfoAttrs(SearchInfoRequest model)
        {

            model.LayerTable();

            return this.ExecMethod(
                () => {
                    return this.Result(this.iInformation.GetInformationAttributes(
                        model.Type,
                        iProjectInfo.Token,
                        model.attributes,
                        model.pagination,
                        model.search,
                        model.isGetComment), model.pagination);
                }, model.IsValid(IsValidEnum.select));
        }



        /// <summary>
        /// 根据属性搜索文章信息
        /// </summary>
        /// <param name="model">查询实体</param>
        /// <returns></returns>
        [HttpPost(".searchbyattrs")]
        [IgnoreAuthorize]
        public IResult PostGetInfoByAttrs(SearchInfoRequest model)
        {

            model.LayerTable();

            return this.ExecMethod(
                () => {
                    return this.Result(this.iInformation.GetInformationByAttributes(
                        iProjectInfo.Token,
                        model.attributes,
                        model.pagination,
                        model.Type,
                        model.search,
                        model.isGetComment), model.pagination);
                }, model.IsValid(IsValidEnum.select));
        }


        /// <summary>
        /// 获取用户发表的信息并指定属性
        /// </summary>
        /// <param name="model">查询实体</param>
        /// <returns></returns>
        [HttpPost(".searchbyuser")]
        public IResult PostGetInfoByUser(SearchInfoRequest model)
        {

            model.LayerTable();

            return this.ExecMethod(
                () => {
                    return this.Result(this.iInformation.GetInformationUser(
                        iClientInfo.Key,
                        iProjectInfo.Token,
                        model.pagination,
                        model.attributes,
                        model.Type,
                        model.search), model.pagination);
                }, model.IsValid(IsValidEnum.select));
        }


        /// <summary>
        /// 根据属性搜索当前用户参与的信息
        /// </summary>
        /// <param name="model">查询实体</param>
        /// <returns></returns>
        [HttpPost(".searchUserByAttrs")]
        public IResult GetInformationUserByAttributes(SearchInfoRequest model)
        {

            model.LayerTable();

            return this.ExecMethod(
                () => {
                    return this.Result(this.iInformation.GetInformationUserByAttributes(
                        iClientInfo.Key,
                        iProjectInfo.Token,
                        model.pagination,
                        model.attributes,
                        model.isGetComment,
                        model.search), model.pagination);
                }, model.IsValid(IsValidEnum.select));
        }



        /// <summary>
        /// 获取用户浏览历史
        /// </summary>
        /// <param name="model">查询实体</param>
        /// <returns></returns>
        [HttpPost(".History")]
        public IResult History(SearchInfoRequest model)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iInformation.GetInformationHistoryUser(
                        iClientInfo.Key,
                        model.Type,
                        iProjectInfo.Token,
                        model.pagination), model.pagination);
                }, model.IsValid(IsValidEnum.select));
        }

        #endregion




        #region Extend Attrs


        /// <summary>
        /// 点赞文章信息
        /// </summary>
        /// <param name="infoKey">文章信息</param>
        /// <returns></returns>
        [HttpPost(".star/{infoKey}")]
        public IResult InfoStar(int infoKey)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iInformation.InformationStar(
                        infoKey,
                        iClientInfo));
                },
                new Tuple<bool, string>(infoKey > 0, infoKey > 0 ? "" : "params: infoKey is null"));
        }


        /// <summary>
        /// 取消点赞文章信息
        /// </summary>
        /// <param name="infoKey">文章信息</param>
        /// <returns></returns>
        [HttpPost(".unstar/{infoKey}")]
        public IResult InfoUnStar(int infoKey)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iInformation.InformationUnStar(
                        infoKey,
                        iClientInfo.Key,
                        iProjectInfo.Token));
                },
                new Tuple<bool, string>(infoKey > 0, infoKey > 0 ? "" : "params: infoKey is null"));
        }


        /// <summary>
        /// 关注文章信息
        /// </summary>
        /// <param name="infoKey">文章信息</param>
        /// <returns></returns>
        [HttpPost(".watch/{infoKey}")]
        public IResult InfoWatch(int infoKey)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iInformation.InformationWatch(
                        infoKey,
                        iClientInfo));
                },
                new Tuple<bool, string>(infoKey > 0, infoKey > 0 ? "" : "params: infoKey is null"));
        }



        /// <summary>
        /// 取消关注文章信息
        /// </summary>
        /// <param name="infoKey">文章信息</param>
        /// <returns></returns>
        [HttpPost(".unwatch/{infoKey}")]
        public IResult InfoUnWatch(int infoKey)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iInformation.InformationUnWatch(
                        infoKey,
                        iClientInfo.Key,
                        iProjectInfo.Token));
                },
                new Tuple<bool, string>(infoKey > 0, infoKey > 0 ? "" : "params: infoKey is null"));
        }



        /// <summary>
        /// 新增阅读数量
        /// </summary>
        /// <param name="infoKey">文章关键字</param>
        /// <param name="count">阅读数量</param>
        /// <returns></returns>
        [HttpPost(".read/{infoKey}/{count}")]
        public IResult InfoSettingRead(int infoKey, int count = 1)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iInformation.InformationRead(
                        infoKey,
                        iClientInfo.Key,
                        count
                        ));
                },
                new Tuple<bool, string>(infoKey > 0, infoKey > 0 ? "" : "params: infoKey is null"));
        }


        #endregion





        #region Info exec

        /// <summary>
        /// 添加文章
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IResult Post(InformationList model)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iInformation.InformationExec(
                        model,
                        SqlExecType.insert));
                }, model.IsValid(IsValidEnum.reg));
        }

        /// <summary>
        /// 修改文章
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        public IResult Put(InformationList model)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iInformation.InformationExec(
                        model,
                        SqlExecType.update));
                }, model.IsValid(IsValidEnum.update));
        }

        /// <summary>
        /// 删除文章信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public IResult Delete(int id)
        {

            return this.ExecMethod(
                () => {
                    return this.Result(this.iInformation.InformationExec(
                        new InformationList
                        {
                            Id = id,
                            ProjectToken = iProjectInfo.Token
                        },
                        SqlExecType.delete));
                },
                new Tuple<bool, string>(id > 0, id > 0 ? "" : "params: id is null"));
        }


        
        /// <summary>
        /// 添加文章属性
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost(".attr")]
        public IResult PostAttr(InformationAttribute model)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iInformation.InformationAttributeExec(
                        model,
                        SqlExecType.insert));
                }, model.IsValid(IsValidEnum.reg));
        }

        /// <summary>
        /// 修改文章属性
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut(".attr")]
        public IResult PutAttr(InformationAttribute model)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iInformation.InformationAttributeExec(
                        model,
                        SqlExecType.update));
                }, model.IsValid(IsValidEnum.update));
        }

        /// <summary>
        /// 删除文章信息属性
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete(".attr{id}")]
        public IResult DeleteAttr(int id)
        {

            return this.ExecMethod(
                () => {
                    return this.Result(this.iInformation.InformationAttributeExec(
                        new InformationAttribute
                        {
                            Id = id
                        },
                        SqlExecType.delete));
                },
                new Tuple<bool, string>(id > 0, id > 0 ? "" : "params: id is null"));
        }
        #endregion


    }
}
